
function addAwards() {
	var html = $("#awards_templete").html();
	var awards = $(html);
	awards.find(".red-bg").click(function () {
		awards.remove();
		$("#awards_list table").each(function () {
			var index = $("#awards_list table").index(this);
			var label = getHanZi(index + 1) + "\u7b49\u5956";
			$(this).find("input[name=\"label\"]").val(label);
			$(this).find(".label").html(label);
		});
	});
	$("#awards_list").append(awards);
	$("#awards_list table").each(function () {
		var index = $("#awards_list table").index(this);
		var label = getHanZi(index + 1) + "\u7b49\u5956";
		$(this).find("input[name=\"label\"]").val(label);
		$(this).find(".label").html(label);
	});
	if ($("#awards_list table").size() == 6) {
		$("#addAwardsBtn").hide();
	}
}
function getHanZi(num) {
	switch (num) {
	  case 1:
		return "\u4e00";
	  case 2:
		return "\u4e8c";
	  case 3:
		return "\u4e09";
	  case 4:
		return "\u56db";
	  case 5:
		return "\u4e94";
	  case 6:
		return "\u516d";
	}
	return "\u8d85\u51fa\u6570\u503c\uff08\u6700\u5927\u53ea\u80fd\u662f6\uff09";
}
function saveActivity() {
	//var editor = $("#myEditor").data("kendoEditor");
	//$("#myEditor").val(editor.value());
	$("#editForm").ajaxSubmit({url:"../lottery/saveActivity.json", type:"post", dataType:"json", success:function (data) {
		if (data.success) {
			var bean = data.attribute["bean"];
			$("activity_id").val(bean.id);
			showMessage("\u4fdd\u5b58\u6210\u529f\uff01");
		}
	}});
}
function editActivity(id) {
	var param = "";
	if (id) {
		param += "?id=" + id;
	}
	$.get("../lottery/editActivity.json" + param, function (html) {
		$("#main_content").html(html);
		
	});
}
function updateActivityState(id, state) {
	$.ajax({url:"../lottery/updateActivityState.json?state=" + state + "&id=" + id, dataType:"json", success:function (data) {
		if (data.success) {
			if(state){
				$("#state"+id).find("span").html('开放').removeClass('yellow-bg ').addClass('blue-bg');
			}else{
				$("#state"+id).find("span").html('关闭').removeClass('blue-bg ').addClass('yellow-bg');
			}
			showMessage("\u4fee\u6539\u6210\u529f\uff01");
		}
	}});
}
function listActivity() {
	$.ajax({url:"activities.json", dataType:"json", success:function (data) {
		$("#contentBody").html("<div id='datagrid'></div>");
		$("#datagrid").kendoGrid({dataSource:{data:data.result, schema:{model:{fields:{id:{type:"number"}, name:{type:"string"}, chance:{type:"number"}, field:{type:"function"}}}}, pageSize:15}, sortable:true, pageable:{refresh:true, pageSizes:true, buttonCount:5}, columns:[{field:"name", title:"\u6d3b\u52a8\u540d\u79f0", width:140}, {field:"chance", title:"\u4e2d\u5956\u673a\u7387", width:190, template:"<span>#= chance*100 #%</span>"}, {field:"open", title:"\u72b6\u6001", template:"<span>#= open==false?'\u5173\u95ed':'\u5f00\u653e' #</span>"}, {command:[{text:"\u5f00\u653e", click:function (e) {
			e.preventDefault();
			var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
			updateActivityState(dataItem.id, true);
		}}, {text:"\u5173\u95ed", click:function (e) {
			e.preventDefault();
			var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
			updateActivityState(dataItem.id, false);
		}}, {text:"\u4fee\u6539", click:function (e) {
			e.preventDefault();
			var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
			editActivity(dataItem.id);
		}}], title:" ", width:"240px"}]});
	}});
}

